<?php

    if(!defined('BASEPATH'))
        exit('No direct script access allowed');

    /*
     * Main excursions platform class
     * 
     */

    class Excursions extends CI_Controller
    {

        var $data;

        public function __construct()
        {

            parent::__construct();

            $this->load->library('rest', array(
                'server' => REST_URL_EXCURSIONS
            ));

            $this->data['hosting']=HOST_URL_EXCURSIONS;
            $this->load->model('cms_pro', 'cms');
            $this->load->library('template');
            $this->load->helper('clean_html');
            $this->load->helper('language');
            $this->load->helper('urlify');
            $this->load->helper('gallery');
            $this->load->helper('week_days');
            // load language file
            $this->lang->load ( 'common' );
            $this->lang->load('excursions');

            require_once("urlify.php");
            $this->load->library('translate', array('lang' => $this->lang->lang()));

            $this->template->set_view("excursions");

            $params=array(
                'lang' => $this->lang->lang()
            );

            // Do caching
            $this->load->driver('cache', array('adapter' => 'file')); // za uklucuvanje na kes mogucnosti

            $cache_file1=$this->crate_cache_filename($params, 'action_1');
            $cache_file2=$this->crate_cache_filename($params, 'action_2');

            if((!$this->data['action_1']=$this->cache->get($cache_file1)) || (!$this->data['action_2']=$this->cache->get($cache_file2)))
            {
                $this->firephp->log($cache_file1);
                $this->firephp->log($cache_file2);

                $this->data['action_1']=$this->rest->post('action1', $params, 'json');
                $this->data['action_2']=$this->rest->post('action2', $params, 'json');

                $this->cache->save($cache_file1, $this->data['action_1'], 300);
                $this->cache->save($cache_file2, $this->data['action_2'], 300);
            }


            shuffle($this->data['action_1']);
            shuffle($this->data['action_2']);

            $this->load->library('session');

        }

        public function index()
        {

            redirect(base_url('excursions'));
            //$this->get_all();
            //echo $this->curl->simple_get($this->server.'excursions?eb_starting=Tuesday&eb_freetext=&exc_sort=none');
            // Debug data ------------------------------------------------
            //print_r($this->curl->info);

        }

        public function home()
        {
            //$this->firephp->log($this->data);
			$this->data['title']= lang('title-home');
			$this->data['description']=lang('description-home');
            $this->template->load('excursions/home', $this->data);

        }

        public function get_all()
        {

            // get days if posted
            $weekdays=array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
            $days_search=array();
            foreach($weekdays as $day) :
                if(isset($_POST[$day]))
                    array_push($days_search, $day);
            endforeach;

            // set params to service
            $params=array(
                'eb_starting' => empty($days_search) ? 'Any' : $days_search,
                'eb_freetext' => isset($_POST['freetext']) ? $_POST['freetext'] : '',
                'exc_sort' => isset($_POST['exc_sort']) ? $_POST['exc_sort'] : 'none',
                'lang' => $this->lang->lang()
            );

            $cache_file=$this->crate_cache_filename($params, 'get_all');

            if(!$this->data['excursions']=$this->cache->get($cache_file))
            {
                $this->firephp->log($cache_file);
                $this->data['excursions']=$this->rest->post('excursions', $params, 'json');

                $this->cache->save($cache_file, $this->data['excursions'], 300);
            }

            $this->data['days_search']=$days_search;

			$this->data['title']=lang('title-search');
			$this->data['description']=lang('description-search');

            $this->template->load('excursions/search', $this->data);

        }

        public function crate_cache_filename($data, $mehod)
        {
            $filename='excursions_'.$mehod.'';

            foreach($data as $key => $value):
                if(is_array($value))
                {
                    foreach($value as $value)
                    {
                        $filename.='_'.$key.'-'.$value;
                    }
                }
                else
                {
                    $filename.='_'.$key.'-'.$value;
                }
            endforeach;

            return $filename;

        }

        public function details($href)
        {

            $excursion_ID=idFromUrl($href, '-');

            $params=array(
                'excursion_id' => $excursion_ID,
                'lang' => $this->lang->lang()
            );

            $this->data['excursion']=$this->rest->get('details', $params, 'json');
            $this->data['images']=$this->rest->get('gallery', $params, 'json');

            //$this->firephp->log($_POST);
            // get days if posted
            $weekdays=array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
            $days_search=array();
            foreach($weekdays as $day) :
                if(isset($_POST[$day]))
                    array_push($days_search, $day);
            endforeach;

            $this->data['days_search']=$days_search;
			
			$this->data['title']= str_replace('{|0|}', $this->data['excursion']->title, lang('title-details'));			
			$this->data['description']= clean_html($this->data['excursion']->description);			
			$this->data['canonical'] = base_url('excursions/'.urlify::filter($this->data['excursion']->title).'-'.$this->data['excursion']->id);

            $this->template->load('excursions/details', $this->data);

        }

        public function check_available()
        {

            $params=array(
                'excid' => $_POST['exc_id'],
                'date' => strtotime($_POST['exc_date']),
                'noadult' => $_POST['adults'],
                'noch' => $_POST['children'],
                'persons' => $_POST['adults'] + $_POST['children'],
                'adultprice' => $_POST['adultprice'],
                'chprice' => $_POST['chprice'],
                'totalprice' => $_POST['totalprice'],
                'pickup_location' => $_POST['location'],
                'lang' => $this->lang->lang()
            );

            //print_r($params);

            $is_available=$this->rest->post('check_capacity', $params, 'json');

            //check if booking is available
            if($is_available->available)
            {

                redirect(base_url('excursions/checkout/'.$is_available->book_info));
            }
            else
            {

                $this->data['booking_msg']='<b>Booking is not available.</b><br />';
                $this->data['booking_msg'] .= 'Place left <b>'.$is_available->place_left.'</b>.';

                $params=array(
                    'excursion_id' => $_POST['id'],
                    'lang' => $this->lang->lang()
                );

                $this->data['excursion']=$this->rest->get('details', $params, 'json');
                $this->data['gallery']=$this->rest->get('gallery', $params, 'json');

                $this->data['title']=$this->data['excursion']->title;
                $this->template->load('excursions/details', $this->data);
            }

        }

        public function checkout($book_info_ID)
        {

            $this->data['title']="Customer Information";

            $params=array(
                'bid' => $book_info_ID,
                'lang' => $this->lang->lang()
            );

            $this->session->set_userdata('temp_bid', $book_info_ID);

            $this->data['booking_info']=$this->rest->get('booking_info', $params, 'json');

            // Get excursion info for checkout
            $params=array(
                'excursion_id' => $this->data['booking_info']->excid,
                'lang' => $this->lang->lang()
            );

            $this->data['excursion']=$this->rest->get('details', $params, 'json');
            $this->data['terms']=$this->translate->trans_row_array($this->cms->get_post(112));

            //$this->firephp->log($this->data);
            $this->template->load('excursions/checkout', $this->data);

        }

        public function checkout_customer()
        {

            // Razlika je u nazivu promjenjljivih u nosiocu rezervacije
            // Na starom sajtu se pravi lista klijenata
            // Na novom sajtu postoji samo jedan klijent
            // Razlicita su imena
            // Zbog ovoga ce se podaci na racuni i mozda na informacionom sistemu na prikazu rezervacija
            $booking=$this->rest->post('do_booking_new', $_POST, 'json');

            //check if booking is insert in db
            if($booking->success)
            {

                echo json_encode(array('response' => TRUE, 'msg' => 'OK', 'json' => $booking));
            }
            else
            {

                echo json_encode(array('response' => FALSE, 'msg' => $booking));
            }

        }

        public function checkout_show_order($orderID)
        {

            $this->data['title']="Booking Information";

            $params=array(
                'fbid' => $orderID,
                'lang' => $this->lang->lang()
            );

            $this->data['order']=$this->rest->get('total', $params, 'json');

            $params=array(
                'bid' => $this->session->userdata('temp_bid'),
                'lang' => $this->lang->lang()
            );

            $this->data['booking_info']=$this->rest->get('booking_info', $params, 'json');
            
            $this->firephp->log ($this->data);

            $this->template->load('excursions/checkout_3', $this->data);

        }

    }

?>
